package com.yizhiliulianta.auth.application.controller;

import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.yizhiliulianta.auth.application.converter.AuthUserDTOConverter;
import com.yizhiliulianta.auth.domain.bo.AuthUserBO;
import com.yizhiliulianta.auth.domain.service.AuthUserDomainService;
import com.yizhiliulianta.auth.entity.AuthUserDTO;
import com.yizhiliulianta.auth.entity.Result;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("/user/")
@Slf4j
public class UserController {

    @Resource
    private AuthUserDomainService authUserDomainService;

    /**
     * 用户注册
     */
    @RequestMapping("register")
    public Result<Boolean> register(@RequestBody AuthUserDTO authUserDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("UserController.register.dto:{}", JSON.toJSONString(authUserDTO));
            }
            Preconditions.checkArgument(!StringUtils.isBlank(authUserDTO.getUserName()), "用户id不能为空");
            Preconditions.checkArgument(!StringUtils.isBlank(authUserDTO.getPassword()), "密码不能为空");
            //吞入前的数据映射
            AuthUserBO authUserBO = AuthUserDTOConverter.INSTANCE
                    .convertDtoToAuthBO(authUserDTO);
            //吞入
            Boolean result = authUserDomainService.register(authUserBO);
            //吐出
            return Result.ok(result);
        } catch (Exception e) {
            log.error("UserController.add.error:{}", e.getMessage(), e);
            return Result.fail("注册用户失败");
        }
    }

    // 会话登录接口
    @RequestMapping("doLogin")
    public Result<SaTokenInfo> doLogin(@RequestParam("validCode") String validCode) {
        try {
            Preconditions.checkArgument(!StringUtils.isBlank(validCode),"验证码不能为空");
            SaTokenInfo tokenInfo = authUserDomainService.doLogin(validCode);
            return Result.ok(tokenInfo);
        }catch (Exception e){
            log.error("UserController.doLogin.error:{}", e.getMessage(), e);
            return Result.fail("用户登录失败");
        }
    }

    /**
     * 修改用户信息
     */
    @RequestMapping("update")
    public Result<Boolean> update(@RequestBody AuthUserDTO authUserDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("UserController.update.dto:{}", JSON.toJSONString(authUserDTO));
            }
            //吞入前的数据映射
            AuthUserBO authUserBO = AuthUserDTOConverter.INSTANCE
                    .convertDtoToAuthBO(authUserDTO);
            //吞入
            AuthUserBO updateUser = authUserDomainService.update(authUserBO);
            AuthUserDTO authDTO = AuthUserDTOConverter.INSTANCE.convertBoToAuthDTO(updateUser);
            //吐出
            return Result.ok(authDTO);
        } catch (Exception e) {
            log.error("UserController.update.error:{}", e.getMessage(), e);
            return Result.fail("修改用户信息失败");
        }
    }

    /**
     * 获取用户信息
     */
    @RequestMapping("getUserInfo")
    public Result<Boolean> getUserInfo(@RequestBody AuthUserDTO authUserDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("UserController.getUserInfo.dto:{}", JSON.toJSONString(authUserDTO));
            }
            //吞入前的数据映射
            AuthUserBO authUserBO = AuthUserDTOConverter.INSTANCE
                    .convertDtoToAuthBO(authUserDTO);
            //吞入
            AuthUserBO updateUser = authUserDomainService.getUserInfo(authUserBO);
            AuthUserDTO authDTO = AuthUserDTOConverter.INSTANCE.convertBoToAuthDTO(updateUser);
            //吐出
            return Result.ok(authDTO);
        } catch (Exception e) {
            log.error("UserController.getUserInfo.error:{}", e.getMessage(), e);
            return Result.fail("获取用户信息失败");
        }
    }

    // 查询登录状态  ---- http://localhost:8081/acc/isLogin
    @RequestMapping("isLogin")
    public SaResult isLogin() {
        return SaResult.ok("是否登录：" + StpUtil.isLogin());
    }
}
